<?php
define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH . '/includes/cls_image.php');
$exc = new exchange($ecs->table('news'), $db, 'id', 'creater');
$image = new cls_image($_CFG['bgcolor']);
/*------------------------------------------------------ */
//-- 框架
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
	//$smarty->assign('ur_here',      $_LANG['18_product_list']);
 	//$smarty->assign('action_link',  array('href' => 'circle.php?act=add', 'text' => $_LANG['02_circle_add']));
 	admin_priv('news_manage');
	$news = get_news_lists();
	$smarty->assign('news_list',  $news['row']);
	$smarty->assign('filter',       $news['filter']);
	$smarty->assign('record_count', $news['record_count']);
	$smarty->assign('page_count',   $news['page_count']);
 	$smarty->assign('full_page',    1);
 	$circle_select = get_circle_select();
 	$smarty->assign("circle_select",$circle_select);
 	$smarty->assign('sort_create_time', '<img src="images/sort_desc.gif">');
 	assign_query_info();
	$smarty->display("news.htm");
}
/*------------------------------------------------------ */
//-- 排序、分页、查询
/*------------------------------------------------------ */
elseif($_REQUEST['act']=="query"){
	$news = get_news_lists();
	$smarty->assign('news_list',  $news['row']);
	$smarty->assign('filter',       $news['filter']);
	$smarty->assign('record_count', $news['record_count']);
	$smarty->assign('page_count',   $news['page_count']);
	$sort_flag  = sort_flag($news['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);
	make_json_result($smarty->fetch('news.htm'), '',array('filter' => $news['filter'], 'page_count' => $news['page_count']));
}
/*------------------------------------------------------ */
//-- 修改上架状态
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'toggle_is_top')
{
	check_authz_json('news_edit');

	$news_id       = intval($_POST['id']);
	$is_top        = intval($_POST['val']);
	if ($exc->edit("is_top = '$is_top' ", $news_id))
	{
		$info = get_user($news_id);
		if($info['is_toped']==0){
			$exc->edit("is_toped = 1", $news_id);
			log_account_change($info['user_id'],0,0,NEWS_TOP,0,sprintf($_LANG['news_add_top_integral'], NEWS_TOP));
		}
		clear_cache_files();
		make_json_result($is_top);
	}
}
elseif ($_REQUEST['act'] == 'audit')
{
	check_authz_json('news_edit');

	$news_id       = intval($_GET['id']);
	$audit_status       = intval($_GET['audit_status']);

	if ($exc->edit("audit_status = '$audit_status' ", $news_id))
	{
		$info = get_user($news_id);
		if($info['is_audited' ]==0){//未审核过的
			$exc->edit("is_audited = 1", $news_id);
			if($audit_status==2){//审核通过+
				log_account_change($info['user_id'],0,0,NEWS_PUBLISH,0,sprintf($_LANG['news_add_integral'], NEWS_PUBLISH));
			}
		}elseif($info['is_audited']==1){//之前审核过的
			if($audit_status==2){//审核通过+
				log_account_change($info['user_id'],0,0,NEWS_PUBLISH,0,sprintf($_LANG['news_add_integral'], NEWS_PUBLISH));
			}else if($audit_status ==3){//审核不通过-
				log_account_change($info['user_id'],0,0,(-1)*intval(NEWS_PUBLISH),0,sprintf($_LANG['news_sub_integral'], NEWS_PUBLISH));
			}
		}
		clear_cache_files();
		make_json_result("审核状态修改成功");
	}
}
elseif ($_REQUEST['act'] == 'remove')
{
	check_authz_json('news_edit');
	$id = intval($_GET['id']);
	$exc->drop($id);
	$url = 'news.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
	ecs_header("Location: $url\n");
	exit;
}
else if($_REQUEST['act'] == 'edit_title')
{
	check_authz_json('news_edit');
	$id    = intval($_POST['id']);
	$title = json_str_iconv(trim($_POST['val']));
	
	if ($exc->edit("title = '$title'", $id))
	{
		clear_cache_files();
// 		admin_log($title, 'edit', 'article');
		make_json_result(stripslashes($title));
	}
	else
	{
		make_json_error($db->error());
	}
}
else if($_REQUEST['act'] == 'edit_order')
{
    check_authz_json('news_edit');
    $id    = intval($_POST['id']);
    $sort_order = json_str_iconv(trim($_POST['val']));

    if ($exc->edit("sort_order = '$sort_order'", $id))
    {
        clear_cache_files();
        // 		admin_log($title, 'edit', 'article');
        make_json_result(stripslashes($sort_order));
    }
    else
    {
        make_json_error($db->error());
    }
}

else if($_REQUEST['act'] == 'editCircle')
{
	check_authz_json('news_edit');
	$id    = intval($_POST['id']);
	$circle_id = json_str_iconv(trim($_POST['circle_id']));
	
	if ($exc->edit("circle_id = '$circle_id'", $id))
	{
		clear_cache_files();
// 		admin_log($title, 'edit', 'article');
		make_json_result(stripslashes($title));
	}
	else
	{
		make_json_error($db->error());
	}
}
function get_news_lists(){
	$result = get_filter();
	if ($result === false)
	{
		$where = " WHERE 1=1 ";
		/* 查询条件 */
// 		$filter['keywords']   = empty($_REQUEST['keywords']) ? '' : trim($_REQUEST['keywords']);
		$filter['circle_id']   = empty($_REQUEST['circle_id']) ? '' : intval($_REQUEST['circle_id']);
		$filter['audit_status']   = empty($_REQUEST['audit_status']) ? '' : intval($_REQUEST['audit_status']);
		$filter['start_time']   = empty($_REQUEST['start_time']) ? '' : trim($_REQUEST['start_time']);
		$filter['end_time']   = empty($_REQUEST['end_time']) ? '' : trim($_REQUEST['end_time']);
		$filter['creater']   = empty($_REQUEST['creater']) ? '' : trim($_REQUEST['creater']);
		if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
		{
			$filter['creater'] = json_str_iconv($filter['creater']);
// 			$filter['audit_status'] = json_str_iconv($filter['audit_status']);
// 			$filter['start_time'] = json_str_iconv($filter['start_time']);
// 			$filter['end_time'] = json_str_iconv($filter['end_time']);
		}
		$filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'create_time' : trim($_REQUEST['sort_by']);
		$filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
		
		$where .= (!empty($filter['circle_id'])) ? " AND p1.circle_id = ". $filter['circle_id'] ." " : '';
		$where .= (!empty($filter['audit_status'])) ? " AND p1.audit_status = ". $filter['audit_status'] ." " : '';
		$where .= (!empty($filter['start_time'])) ? " AND p1.create_time >=". strtotime($filter['start_time'].":00") ."" : '';
		$where .= (!empty($filter['end_time'])) ? " AND p1.create_time <= ". strtotime($filter['end_time'].":59") ."" : '';
		$where .= (!empty($filter['creater'])) ? " AND p3.nickname like '%". mysql_like_quote($filter['creater']) ."%'" : '';
	
		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('news')." p1 ". $where;
		$filter['record_count'] = $GLOBALS['db']->getOne($sql);
	
		$filter = page_and_size($filter);
	
		/* 获活动数据 */
		$sql = "SELECT p1.*,p2.circle_name,p3.nickname FROM " .
				 $GLOBALS['ecs']->table('news')." p1 ".
				 " LEFT JOIN ".$GLOBALS['ecs']->table('circle')." p2 ON p1.circle_id = p2.id  " .
				 " LEFT JOIN `wxch_user` p3 ON p1.create_id = p3.uid ".
				  $where.
				" ORDER by p1.$filter[sort_by] $filter[sort_order] LIMIT ". $filter['start'] .", " . $filter['page_size'];
		//$filter['keywords'] = stripslashes($filter['keywords']);
		set_filter($filter, $sql);
	}
	else
	{
		$sql    = $result['sql'];
		$filter = $result['filter'];
	}
	$row = $GLOBALS['db']->getAll($sql);
	//获取
	foreach ($row as $k=>$v){
		$row[$k]['create_time'] = date($GLOBALS['_CFG']['time_format'], $v['create_time']);
		switch ($v['audit_status'])
		{
			case 1:
				$row[$k]['audit_status_show'] = "未审核";
				break;
			case 2:
				$row[$k]['audit_status_show'] = "审核通过";
				break;
			case 3:
				$row[$k]['audit_status_show'] = "审核不通过";
				break;
		}

		$sql = "SELECT thumb FROM ".$GLOBALS['ecs']->table('news_img')." WHERE news_id = ".$v['id'];
		$img = $GLOBALS['db']->getAll($sql);
		$row[$k]['img'] = $img;
	}
	
	$arr = array('row' => $row, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
	return $arr;
	
}

function get_circle_info($id){
	$where = " WHERE id='$id'";
	/* 获得活动数据 */
	$sql = "SELECT * FROM " . $GLOBALS['ecs']->table('circle') . $where;
	$data = $GLOBALS['db']->GetRow($sql);
	$data['circle_img'] = "/data/afficheimg/".$data['circle_img'];
	return $data;
}

function get_circle_select(){
	$sql = "SELECT id,circle_name FROM ".$GLOBALS['ecs']->table('circle');
	$selects = $GLOBALS['db']->getAll($sql);
	return $selects;
}

function get_user($id){
	$sql = "SELECT create_id,is_toped,is_audited FROM ".$GLOBALS['ecs']->table('news')." WHERE id = $id ";
	$info = $GLOBALS['db']->getRow($sql);
	$sql = "SELECT p1.user_id FROM ".$GLOBALS['ecs']->table('users')." p1 ".
			" LEFT JOIN `wxch_user` p2 ON p1.wxid = p2.wxid WHERE p2.uid = ".$info['create_id']." LIMIT 1 ";
	$user_id = $GLOBALS['db']->getOne($sql);
	$info['user_id'] = $user_id;
	return $info;
}